﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PServiceBus.Core.Runtime.Serializers;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Runtime.Extensions;
using PServiceBus.Serializer.Xml;

namespace PServiceBus.Tests.Unit {
    [TestClass]
    public class XmlSerializerTest {

        public class EmptyClass { }

        public class ClassWithStringNumber {
            public string Text { get; set; }
        }

        [TestMethod]
        public void EmptyClassShouldDeserializeSuccessfully() {
            var emptyClass = new EmptyClass();
            var emptyObj = default(EmptyClass);
            var xml = XmlSerializer.Serialize(emptyClass);
            var ex = MethodHelper.Try(() =>
                emptyObj = XmlSerializer.Deserialize<EmptyClass>(xml));
            Assert.IsNull(ex, "ex is not null");
            Assert.IsNotNull(emptyObj, "emptyObj is null");
        }

        [TestMethod]
        public void TestClassWithStringNumber() {
            var obj = new ClassWithStringNumber { Text = "123" };
            var xml = XmlSerializer.Serialize(obj);
            var obj2 = XmlSerializer.Deserialize<ClassWithStringNumber>(xml);

            var json = JsonSerializer.XmlToJson(xml);
            var dict = json.FromJson<Dictionary<string, object>>();

            var typeFromDict = ReflectionHelper.GenerateClassType("JsonClass", dict);

            dynamic obj3 = XmlSerializer.Deserialize(xml, typeFromDict);

            Assert.IsTrue(obj3 != null && obj3.Text == obj2.Text);
        }
    }
}
